
using System;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;

//SVN Ref
using org.tmatesoft.svn.core;
using org.tmatesoft.svn.core.wc;


namespace Subsync.Core.Subversion
{

	public class SVNPropList : CommandBase
	{
		public SVNRevision peg_revision = SVNRevision.UNDEFINED;
		public SVNRevision revision = SVNRevision.HEAD;
		java.io.File localpath;
		SVNURL repospath;
		bool WConly;

		public SVNPropList () // Empty
		{
		}
		
		public SVNPropList(string working_path, SVNRevision peg_rev, SVNRevision target_rev)
		{
			Debug.WriteLine("SVN proplist std constructor");
			WConly = this.checkPath(working_path);
			if (WConly){
				localpath = new java.io.File(working_path);
				repospath = null;
			}
			else{
				localpath = null;
				repospath = SVNURL.parseURIEncoded(working_path);
			}
			peg_revision = peg_rev;
			revision = target_rev;
		}
		
		public override void Execute()
		{
			Debug.WriteLine("SVN propget Execution");
			
					
			// Setup library
			Tools.setupLibrary();
			
			// Setup clients
			SVNClientManager cm = SVNClientManager.newInstance();
			SVNWCClient wcclient = cm.getWCClient();
			PropGetHandler handle = new PropGetHandler();
			
			try {
				if (WConly){ // input path leads to sth in WC					
					wcclient.doGetProperty(localpath,null,peg_revision,revision,SVNDepth.EMPTY,handle,null);
				}else{ // input path leads to sth in repos
					wcclient.doGetProperty(repospath,null,peg_revision,revision,SVNDepth.EMPTY,handle);
				}
				
			} catch (SVNException exptn) {
				throw new SVNExecuteException(exptn.getMessage());
			}
			
		}
		
		public override Dictionary<string, List<object>> getTokens ()
		{
			Dictionary<string, List<object>> ret = new Dictionary<string, List<object>> ();
			ret.Add("proplist",new List<object>() {"pl","proplist"});
			return ret;
		}
		
		public override List<KeyValuePair<string,initPointer>> getInitPointers()
		{
			List<KeyValuePair<string,initPointer>> ret = new List<KeyValuePair<string,initPointer>>();
			ret.Add(new KeyValuePair<string,initPointer>("proplist",new initPointer(this.handleCLI)));
			return ret;
		}
		
		public void handleCLI(Dictionary<string,List<object>> tokens)
		{
			if (tokens["proplist"].Count!=1){
				Console.WriteLine("Invalid arguments");
				return;
			}
			
			this.setupPaths((string)tokens["proplist"][0]);
			this.Execute();
		}
		
		public bool checkPath(string inputPath)
		{
			// Checks if path is for repos access or WC access
			// Returns true for WC access (used for bool member WCOnly)
			Debug.WriteLine("SVN proplist method to check if input path is repos access");
			List<string> repos_access = new List<string>();
			// List of Repos access URLs
			repos_access.Add("file:///"); // Local repos
			repos_access.Add("http://");
			repos_access.Add("https://");
			repos_access.Add("svn://");
			repos_access.Add("svn+ssh://");
			
			foreach (string accessprefix in repos_access)
			{
				if (inputPath.StartsWith(accessprefix))
					return false;
			}
			
			return true;
		}
		
		private void setupPaths(string input_path)
		{
			Debug.WriteLine("SVN proplist attribute setup");
			WConly = this.checkPath(input_path);
			if (WConly){
				localpath = new java.io.File(input_path);
				repospath = null;
			}
			else{
				localpath = null;
				repospath = SVNURL.parseURIEncoded(input_path);
			}
			
		}
		
	}
}
